#include <Nordic\reg24le1.h>
#include <stdint.h>
#include "hal_spi.h"

#define CHIP_SELECT P05

#define LED_PORT P0
#define LED_PORT_DIR P0DIR
#define BUTTON_PORT P0
#define BUTTON_PORT_DIR P0DIR

#define SPI_PACKET_LENGTH 3

void delay(uint16_t del)
{
  xdata uint16_t rall = del;
  while(rall--);
}
void main()
{
  uint8_t i;
  uint8_t spibuf_out[SPI_PACKET_LENGTH], spibuf_in[SPI_PACKET_LENGTH];

  BUTTON_PORT_DIR = 0xff;
  LED_PORT_DIR = 0x00;

  P0DIR = 0x00;
  CHIP_SELECT = 1;

  hal_spi_master_init(SPI_CLK_DIV8, HAL_SPI_MODE_1, HAL_SPI_LSB_MSB);

  while(1)
  {
    for( i = 0; i < SPI_PACKET_LENGTH; i++ )
    {
        spibuf_out[i] = i+1;
    }

    // SPI transaction
    CHIP_SELECT = 0;
    for( i = 0; i < SPI_PACKET_LENGTH; i++ )
      spibuf_in[i] = hal_spi_master_read_write( spibuf_out[i] );
    CHIP_SELECT = 1;

    delay(30);
  }  
} 
   